define(function (require) {

    require('../../../c/base/index');

    require('../../../c/nav/index');


    //command
    var orderCommand = require('../../../c/juicer/order');
    orderCommand.init();
    var orderDetail = require('../../../c/order-detail/index');
    var firstTime = true;

    var rate = {
        init: function () {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function () {
                if ($(this).parents("a").text() == "评价查询") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.initTable();
            this.initValidator();
            this.bindEvent();
            this.initDatetimepicker();
        },
        /**
         * 执行查询操作.
         */
        doSearch: function () {
            $('#table').DataTable().ajax.reload();
        },
        /**
         * 初始化列表
         */
        initTable: function () {
            var self = this;
            $("#table").dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%",   //表格的宽度
                "sScrollXInner": "100%",   //表格的内容宽度
                "bScrollCollapse": true,  //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": true,  //是否显示分页
                "bLengthChange": false,  //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": false, //显示表格信息
                "bAutoWidth": false,  //自适应宽度
                "bServerSide": true,//开启服务端处理
                "sAjaxSource": "../../crm/member/searchRate",
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    if (firstTime) {
                        firstTime = false;
                        return;
                    }
                    var dataString, _data = {};
                    $($("#J_Search").serializeArray()).each(function () {
                        _data[this.name] = this.value;
                    });
                    _data.pageIndex = aoData[3].value / aoData[4].value + 1;
                    _data.pageSize = aoData[4].value;
                    if (_data.fromDate) {
                        _data.fromDate = _data.fromDate + " 00:00:00";
                    }
                    if (_data.toDate) {
                        _data.toDate = CRM.formatDate(new Date(new Date(_data.toDate).getTime() + 1 * 86400000), "date") + " 00:00:00";
                        ;
                    }
                    dataString = JSON.stringify(_data);
                    CRM.request({
                        "type": 'POST',
                        "url": sSource,
                        "dataType": "json",
                        contentType: "application/json",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": dataString,
                        contentType: "application/json",
                        "success": function (req) {
                            if (req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    resultData.iTotalDisplayRecords = 9999999999;
                                    resultData.iTotalRecords = 9999999999;
                                    $.each(req.entry, function (key, entry) {
                                        resultData.aaData.push(entry)
                                    });
                                } else {
                                    CRM.showDialog(req.message);
                                }
                                fnCallback(resultData);
                            } else {
                                CRM.showDialog(req.message);
                            }
                        }
                    });
                },
                "aaSorting": [],  //给列表排序 ，第一个参数表示数组 (由0开始)。1 表示Browser列。第二个参数为 desc或是asc
                "aoColumns": [
                    {
                        "sTitle": "评价时间",
                        "mDataProp": "gmtCreate",
                        "bSortable": false
                    },
                    {
                        "sTitle": "城市",
                        "mDataProp": "bizOrderDO.cityName",
                        "bSortable": false,
                        "mRender": function (data, type, row) {
                            var _html = "";
                            if (data) {
                                if (row.bizOrderDO.attributesMap.buyerCity) {
                                    _html = row.bizOrderDO.attributesMap.buyerCity;
                                } else {
                                    _html = data;
                                }
                            }
                            return _html;
                        }
                    },
                    {
                        "sTitle": "订单ID",
                        "mDataProp": "bizOrderId",
                        "bSortable": false,
                        "mRender": function (data, type, row) {
                            var _html = "<a href='javascript:openOrderDetail(\"" + data + "\")'>" + data + "</a>";
                            return _html;
                        }
                    },
                    {
                        "sTitle": "订单类型",
                        "mDataProp": "bizOrderDO.orderType",
                        "bSortable": false,
                        "mRender": function (data, type, row) {
                            var _html = "";
                            if (data == 1) {
                                _html = "普通订单";
                            } else if (data == 2) {
                                _html = "拼团订单";
                            } else if (data == 3) {
                                _html = "分销订单";
                            } else if (data == 4) {
                                _html = "预售订单";
                            } else if (data == 5) {
                                _html = "次日达订单";
                            }
                            return _html;
                        }
                    },
                    {
                        "sTitle": "评价类型",
                        "mDataProp": "type",
                        "bSortable": false,
                        "mRender": function (data, type, row) {
                            var _html = "";
                            if (data == 1) {
                                _html = "差评";
                            } else if (data == 2) {
                                _html = "追加评论";
                            } else {
                                _html = "好评";
                            }
                            return _html;
                        }
                    },
                    {
                        "sTitle": "评价内容",
                        "mDataProp": "comment",
                        "bSortable": false,
                        "sWidth": "60%",
                        "mRender": function (data, type, row) {
                            var _html = "";
                            if (data) {
                                _html = data;
                            }
                            return _html;
                        }
                    }


                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "simple" //分页，一共两种样式，full_numbers和two_button(默认)
            });
        },
        /**
         * 打开订单详情
         * @param id
         */
        openOrderDetail: function (id) {
            orderDetail.init(id);
        },
        /**
         * 表单验证初始化
         */
        initValidator: function () {
            $('#J_Search').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {}
            });

        },
        /**
         * 初始化日期控件
         */
        initDatetimepicker: function () {
            $(".form_time").datetimepicker({
                language: 'zh-CN',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 1,
                minView: 0,
                maxView: 1,
                forceParse: 0
            });
            $('.form_date').datetimepicker({
                language: 'zh-CN',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0
            });
        },
        bindEvent: function () {
            var self = this;
            $("#search_btn").on('click', function () {
                self.doSearch();
            });

        }
    };

    window.doSearch = $.proxy(rate.doSearch, rate);
    window.openOrderDetail = $.proxy(rate.openOrderDetail, rate);

    rate.init();
});